package cn.cdg.mall.service.impl;

import cn.cdg.mall.mapper.PersonMapper;
import cn.cdg.mall.mapper.UpMapper;
import cn.cdg.mall.mapper.UserMapper;
import cn.cdg.mall.pojo.po.PersonPO;
import cn.cdg.mall.pojo.po.UpPO;
import cn.cdg.mall.pojo.po.UserPO;
import cn.cdg.mall.service.PersonService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private UpMapper upMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean addPerson(PersonPO person) {
        System.out.println("Before Person!!");
        int count1 = personMapper.insert(person);
        System.out.println("Person!!");
        return count1 > 0;
    }

    @Override
    public boolean addUp(UpPO up) {
        System.out.println("Before Up!!");
        int count2 = upMapper.insert(up);
        System.out.println("Up!!");
        return count2 > 0;
    }

    @Override
    public List<PersonPO> selectAllPerson(UserPO userPO) {
        QueryWrapper<UpPO> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_no", userPO.getUserNo());
        List<UpPO> ups = upMapper.selectList(queryWrapper1);
        List<String> ids = ups.stream().map(UpPO::getID).collect(Collectors.toList());
        return personMapper.selectBatchIds(ids);
    }

    @Override
    public List<UpPO> selectAllUp() {
        List<UpPO> ups = upMapper.selectList(null);
        return ups;
    }
}